Skip to content

fix: prevent caching during vary transition#127

Open
furan917 wants to merge 1 commit into
elgentos:masterfrom
furan917:fix/vary-cookie-transition-caching
Open

fix: prevent caching during vary transition#127
furan917 wants to merge 1 commit into
elgentos:masterfrom
furan917:fix/vary-cookie-transition-caching

Conversation

@furan917

@furan917 furan917 commented Apr 2, 2026

Copy link
Copy Markdown

When a visitor switches currency or language (or other vary object someone may set), Magento issues a new X-Magento-Vary cookie value. Varnish would strip the Set-Cookie header and cache the response regardless, meaning the browser never received the updated cookie and subsequent requests continued hitting the wrong cached variant for their old context and poisoning cache for others.

This change detects when the X-Magento-Vary value the backend wants to set differs from the one the visitor sent, marks the response uncacheable, and passes Set-Cookie through to the browser so it receives the correct vary hash. Normal requests where the vary value is unchanged are unaffected.

A VTC test covers all four scenarios: vary unchanged, vary changed, first-time visitor (no existing vary cookie), and vary present in the request with no Set-Cookie in the response.

@furan917

furan917 commented Apr 3, 2026

Copy link
Copy Markdown
Author

I think the delay on the grace.vtc needs a slightly larger delay than 2s, the varnish:fresh is pulling in varnish v9 so probably quicker.

@toonvd

toonvd commented May 12, 2026

Copy link
Copy Markdown
Collaborator

Language switches and other things that change the vary cookie should never happen on GET or HEAD requests. It's best to refrain from trying to fix bad architecture in a widely used VCL. It would be the start of chaos.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants